#ifndef BIBLEREADERDOWNLOADER_H
#define BIBLEREADERDOWNLOADER_H

#include <QObject>
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkReply>
#include <QNetworkRequest>

#define BLOCK 1
#define ASYNC 0

class BibleReaderDownloader : public QObject
{
    Q_OBJECT
public:
    explicit BibleReaderDownloader(QUrl &url, QObject *parent = nullptr);
    explicit BibleReaderDownloader(QString &urlstr, QObject *parent = nullptr);
    ~BibleReaderDownloader();

    QByteArray getData() const;

    QUrl getUrl() const;
    void setUrl(const QUrl &value);

    bool getIsFinished() const;

    QNetworkReply::NetworkError getErrorCode() const;

    void setErrorCode(const QNetworkReply::NetworkError &value);

private:
    QUrl url;
    QNetworkAccessManager *manager;
    QNetworkReply *reply;
    QByteArray data;
    QString errorString;
    QNetworkReply::NetworkError errorCode;
    bool isFinished;

signals:
    void finished();
    void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);

public slots:
    void start(int block = ASYNC);

private slots:
    virtual void replyFinished(QNetworkReply *reply);
    void proccessError(QNetworkReply::NetworkError code);
    void processDownloadProgress(qint64 bytesReceived, qint64 bytesTotal);

};

#endif // BIBLEREADERDOWNLOADER_H
